/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.controller;

import cn.platform.entity.Essay;
import cn.platform.response.ResponseData;
import cn.platform.response.SuccessResponseData;
import cn.platform.service.IEssayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private IEssayService essayService;

    /**
     * 发布文章
     * @param essay 文章
     * @return 提交结果
     */
    @RequestMapping(value = "/release", method = RequestMethod.POST)
    public ResponseData releaseArticle(@RequestBody Essay essay){
        return new SuccessResponseData(essayService.releaseArticle(essay));
    }

    /**
     * 提交文章为草稿
     * @param essay 文章
     * @return 提交结果
     */
    @RequestMapping(value = "/draft", method = RequestMethod.POST)
    public ResponseData draftArticle(@RequestBody Essay essay){
        return new SuccessResponseData(essayService.draftArticle(essay));
    }

    /**
     * 其他用户查看作者的文章
     * @param authorId 作者id
     * @return 文章列表
     */
    @RequestMapping(value = "subscriber", method = RequestMethod.GET)
    public ResponseData findArticleByAuthorId(@RequestParam(value = "authorId") Long authorId){
        System.out.println(authorId);
        return new SuccessResponseData(essayService.findArticleListByAuthorId(authorId));
    }

    /**
     * 作者本人查看自己的文章列表
     * @param authorId 作者id
     * @return 文章列表
     */
    @RequestMapping(value = "principle", method = RequestMethod.GET)
    public ResponseData findArticleListByAuthorIdPrinciple(@RequestParam(value = "authorId") Long authorId){
        return new SuccessResponseData(essayService.findArticleListByAuthorIdPrinciple(authorId));
    }

    /**
     * 其他用户通过文章类别查看其他作者的文章
     * @param classifications 类别
     * @return 文章列表
     */
    @RequestMapping(value = "subscriber/classifications", method = RequestMethod.GET)
    public ResponseData saveArticleByClassifications(@RequestParam(value = "classifications") String... classifications){
        return new SuccessResponseData(essayService.findArticleListByClassification(classifications));
    }

    /**
     * 其他用户通过标签查看其他作者的文章
     * @param labels 标签
     * @return 文章列表
     */
    @RequestMapping(value = "subscriber/labels", method = RequestMethod.GET)
    public ResponseData saveArticleByLabels(@RequestParam(value = "labels") String... labels){
        return new SuccessResponseData(essayService.findArticleListByLabel(labels));
    }

    @RequestMapping(value = "recommend")
    public ResponseData articleRecommend(){
        return new SuccessResponseData(essayService.findArticleRecommend());
    }

    @RequestMapping(value = "/{articleId}")
    public ResponseData getArticleById(@PathVariable("articleId") Long articleId){
        return new SuccessResponseData(essayService.getById(articleId));
    }

    @RequestMapping(value = "/headline")
    public ResponseData getArticleByHeadline(@RequestParam("headline") String headline){
        return new SuccessResponseData(essayService.findArticleByHeadline(headline));
    }
}
